package com.stormrage.edu.serv.edu.impl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.stormrage.edu.dao.edu.EduCourseDao;
import com.stormrage.edu.dao.learn.LeaStudentDao;
import com.stormrage.edu.serv.edu.EduCourseServ;
import com.stormrage.edu.serv.learn.LeaStudentVideoServ;
import com.stormrage.edu.serv.learn.LeaTermServ;
import com.stormrage.entity.edu.EduCourse;
import com.stormrage.entity.edu.rep.EduCourseRep;
import com.stormrage.entity.learn.LeaStudent;
import com.stormrage.entity.learn.LeaTerm;
import com.stormrage.entity.learn.rep.LeaCourseStudyRep;
import com.stormrage.entity.learn.rep.LeaStudentVideoRep;
import com.stormrage.entity.learn.req.LeaCourseStudyReq;
import com.stormrage.entity.learn.req.LeaStudentVideoReq;
import com.stormrage.entity.learn.view.LeaStudentVideo;
import com.stormragetech.core.exception.StormrageTechException;
import com.stormragetech.core.util.NumberUtil;

/**
 * @Title: EduCourseServImpl.java
 * @Package com.stormrage.edu.serv.edu.impl
 * @Description: 课程 Service
 * @author www.stormragetech.com
 * @date 2016年4月17日
 * @version V1.0
 */
@Service
public class EduCourseServImpl implements EduCourseServ {

	private static final Logger logger = LoggerFactory.getLogger(EduCourseServImpl.class);

	@Autowired
	private EduCourseDao eduCourseDao;

	@Autowired
	private LeaStudentDao leaStudentDao;

	@Autowired
	private LeaStudentVideoServ leaStudentVideoServ;

	@Autowired
	private LeaTermServ leaTermServ;

	@Override
	public EduCourse queryById(final Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】-【 EduCourseServ 】-【 QueryById 】-【 STARTED 】");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", arg0);

		EduCourse entity = eduCourseDao.queryById(map);

		logger.debug(" 【 Serv 】-【 EduCourseServ 】-【 QueryById 】-【 COMPLETED 】");
		return entity;

	}

	@Override
	public EduCourseRep queryRepById(Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】-【 EduCourseServ 】-【 queryRepById 】-【 STARTED 】");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", arg0);

		EduCourseRep entity = eduCourseDao.queryRepById(map);

		logger.debug(" 【 Serv 】-【 EduCourseServ 】-【 queryRepById 】-【 COMPLETED 】");
		return entity;

	}

	@Override
	public List<EduCourse> queryListByMajorId(final Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】-【 EduCourseServ 】-【 QueryListByMajorId 】-【 STARTED 】");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("majorId", arg0);

		List<EduCourse> array = eduCourseDao.queryListByMajorId(map);

		logger.debug(" 【 Serv 】-【 EduCourseServ 】-【 QueryListByMajorId 】-【 COMPLETED 】");
		return array;

	}

	@Override
	public List<EduCourseRep> queryRepListByMajorId(final Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】-【 EduCourseServ 】-【 queryRepListByMajorId 】-【 STARTED 】");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("majorId", arg0);

		List<EduCourseRep> array = eduCourseDao.queryRepListByMajorId(map);
		for (int i = 0; i < array.size(); i++) {
			EduCourseRep course = array.get(i);
			if (i % 3 == 2)
				course.setRight(0);
			else
				course.setRight(1);
		}

		logger.debug(" 【 Serv 】-【 EduCourseServ 】-【 queryRepListByMajorId 】-【 COMPLETED 】");
		return array;

	}

	@Override
	public List<EduCourseRep> queryRepListForSelect(final Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】-【 EduCourseServ 】-【 queryRepListForSelect 】-【 STARTED 】");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("majorId", arg0);

		List<EduCourseRep> array = eduCourseDao.queryRepListForSelect(map);
		int seq = 1;

		Iterator<EduCourseRep> it = array.iterator();
		while (it.hasNext()) {
			EduCourseRep entity = it.next();
			entity = EduCourseRep.instance(entity, seq);
			seq++;
		}

		logger.debug(" 【 Serv 】-【 EduCourseServ 】-【 queryRepListForSelect 】-【 COMPLETED 】");
		return array;

	}

	@Override
	public List<LeaCourseStudyRep> queryStudyRepListByMajorId(final LeaCourseStudyReq arg0)
			throws StormrageTechException {

		logger.debug(" 【 Serv 】-【 EduCourseServ 】-【 queryStudyRepListByMajorId 】-【 STARTED 】");

		LeaTerm term = leaTermServ.queryById(arg0.getTermId());

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("acctId", arg0.getAcctId());
		map.put("majorId", term.getMajorId());

		List<LeaCourseStudyRep> array = eduCourseDao.queryStudyRepListByMajorId(map);
		LeaStudent stu = leaStudentDao.queryByAcctMajor(map);
		int seq = 1;

		Iterator<LeaCourseStudyRep> it = array.iterator();
		while (it.hasNext()) {
			LeaCourseStudyRep entity = it.next();

			LeaStudentVideoReq req = LeaStudentVideoReq.instance(stu.getId(), entity.getId(), null);
			List<LeaStudentVideoRep> videoArray = leaStudentVideoServ.queryRepListByStuCourse(req);

			String study = "0%";
			Integer studyLen = 0;
			Integer totalLen = 0;

			if (null != videoArray && videoArray.size() > 0) {

				Iterator<LeaStudentVideoRep> iter = videoArray.iterator();
				while (iter.hasNext()) {
					LeaStudentVideoRep rep = iter.next();
					if (null != rep.getLen()) {
						studyLen += rep.getLen();
					}
					totalLen += rep.getTlen();
				}

				study = NumberUtil.initPerc(studyLen, totalLen, "%");
			}

			entity = LeaCourseStudyRep.instance(entity, seq, videoArray.size(), study, seq % 3 == 0 ? 0 : 1);
			seq++;
		}

		logger.debug(" 【 Serv 】-【 EduCourseServ 】-【 queryStudyRepListByMajorId 】-【 COMPLETED 】");
		return array;

	}

	@Override
	public List<LeaCourseStudyRep> queryStudyRepList(final LeaCourseStudyReq arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】-【 EduCourseServ 】-【 queryStudyRepList 】-【 STARTED 】");

		LeaTerm term = leaTermServ.queryById(arg0.getTermId());

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("acctId", arg0.getAcctId());
		map.put("majorId", term.getMajorId());
		map.put("ness", arg0.getNess());

		List<LeaCourseStudyRep> array = eduCourseDao.queryStudyRepList(map);
		int seq = 1;

		Iterator<LeaCourseStudyRep> it = array.iterator();
		while (it.hasNext()) {
			LeaCourseStudyRep entity = it.next();

			LeaStudentVideoReq req = LeaStudentVideoReq.instance(entity.getStuId(), entity.getId(), null);
			List<LeaStudentVideoRep> videoArray = leaStudentVideoServ.queryRepListByStuCourse(req);

			String study = "0%";
			Integer studyLen = 0;
			Integer totalLen = 0;
			logger.debug(" time res is >>>>> " + videoArray.toString());

			if (null != videoArray && videoArray.size() > 0) {

				Iterator<LeaStudentVideoRep> iter = videoArray.iterator();
				while (iter.hasNext()) {
					LeaStudentVideoRep rep = iter.next();
					if (null == rep.getLen()) {
						LeaStudentVideo stuVideo = leaStudentVideoServ.queryById(rep.getId());
						stuVideo.setTime(0);
						leaStudentVideoServ.update(stuVideo);
						rep.setLen(0);
					}
					if (rep.getLen() > rep.getTlen()) {
						LeaStudentVideo stuVideo = leaStudentVideoServ.queryById(rep.getId());
						stuVideo.setTime(rep.getTlen());
						leaStudentVideoServ.update(stuVideo);
					}

					studyLen += rep.getLen();
					totalLen += rep.getTlen();
				}

				study = NumberUtil.initPerc(studyLen, totalLen, "%");
			}

			entity = LeaCourseStudyRep.instance(entity, seq, videoArray.size(), study, seq % 3 == 0 ? 0 : 1);
			seq++;
		}

		logger.debug(" 【 Serv 】-【 EduCourseServ 】-【 queryStudyRepList 】-【 COMPLETED 】");
		return array;

	}

}
